package com.hongdee.scm.allocationorder.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.allocationorder.dao.ITScmAllocationOrderDao;
import com.hongdee.scm.allocationorder.entity.TScmAllocationOrder;
import com.hongdee.scm.allocationorder.entity.TScmAllocationOrderDetail;
import com.hongdee.scm.allocationorder.service.ITScmAllocationOrderService;
import com.hongdee.scm.common.service.ICommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/5/15.
 */
@Service
public class TScmAllocationOrderServiceImpl implements ITScmAllocationOrderService {

    @Autowired
    private ICommonService common;
    @Autowired
    private ITScmAllocationOrderDao itScmAllocationOrderDao;
    @Autowired
    private IHibernateDao iHibernateDao;
    private String tabName = "t_scm_allocation_order";


    /**
     * 调拨分页
     *
     * @param data
     * @param start
     * @param limit
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Map page(Map data, String start, String limit, String uid) throws Exception {

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("select * from (select p.*,substr(p.submit_date,1,10) submit_date1,substr(p.finish_date,1,10) finish_date1,tbu.u_name  " +
                "from " + tabName + " p left join t_bc_user tbu  on p.applicant = tbu.id where p.company_id=? ");
        params.add(TokenUtil.getCompanyId(uid));
        //是否是管理员
        String resRoleSQL = "select distinct r.id,r.is_manger from t_bc_role r ,t_bc_role_user ru where r.id = ru.r_id and ru.u_id=?";
        List<Map<String, Object>> rseList = this.itScmAllocationOrderDao.list(resRoleSQL, uid);
        boolean isManger = false;
        for (Map<String, Object> temp : rseList) {
            if ("1".equals(String.valueOf(temp.get("isManger")))) {
                isManger = true;
            }
        }
        //调拨状态
        if (data.get("allocationStatus") != null && !data.get("allocationStatus").equals("")) {
            if(data.get("allocationStatus").equals("0")){ //已创建
                sql.append(" and applicant is null and submit_date is null");
            }
            if(data.get("allocationStatus").equals("1")){//已提交
                sql.append(" and submit_date is not null and EXISTS (select circulation_status from t_scm_approvalflow f" +
                        " where p.current_approval_status = f.circulation_status and f.status ='1'" +
                        " and f.start_status = '开始') ");
            }
            if(data.get("allocationStatus").equals("2")){ //已结束
                sql.append(" and current_approval_status = '结束' ");
            }
        }
        //申请日期起
        if (data.get("applicationStartDate") != null && !data.get("applicationStartDate").equals("")) {
            sql.append(" and substr(p.submit_date,1,10) >= ? ");
            params.add(data.get("applicationStartDate"));
        }
        //申请日期止
        if (data.get("applicationStopDate") != null && !data.get("applicationStopDate").equals("")) {
            sql.append(" and substr(p.submit_date,1,10) <= ? ");
            params.add(data.get("applicationStopDate"));
        }
        //发出仓库
        if (data.get("fromWarehouse") != null && !data.get("fromWarehouse").equals("")) {
            sql.append(" and from_warehouse = ? ");
            params.add(data.get("fromWarehouse"));
        }
        //接受仓库
        if (data.get("aimWarehouse") != null && !data.get("aimWarehouse").equals("")) {
            sql.append(" and aim_warehouse = ? ");
            params.add(data.get("aimWarehouse"));
        }
        //vin查询
        if (data.get("vin") != null && !data.get("vin").equals("")) {
            sql.append(" and EXISTS (select d.allocation_order_id from t_scm_allocation_order_detail d where d.allocation_order_id = p.id and vin like ? ) ");
            params.add("%"+data.get("vin")+"%");
        }
        //是否是管理员
        if (isManger) {//管理员
            if (data.get("saId") != null && !StringUtils.isEmpty(data.get("saId"))) {
                //选择SA并且是管理员
                sql.append(" and p.create_user = ? ");
                params.add(data.get("saId"));
            }
        } else {
            if (data.get("saId") != null && !StringUtils.isEmpty(data.get("saId"))) {
                if (!uid.equals(data.get("saId"))) {
                    throw new Exception("请选择管理员账号登录！");
                } else {
                    sql.append(" and p.create_user = ? ");
                    params.add(uid);
                }
            } else {
                sql.append(" and p.create_user = ? ");
                params.add(uid);
            }
        }
        sql.append(" order by p.create_time desc) temp");
        return this.itScmAllocationOrderDao.page(sql.toString(), params.toArray(), start, limit);
    }

    /**
     * 给财务的单独权限(查询该公司的所有调拨)
     * @param data
     * @param start
     * @param limit
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Map pageFinance(Map data, String start, String limit, String uid) throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("select p.*,substr(p.submit_date,1,10) submit_date1,substr(p.finish_date,1,10) finish_date1,tbu.u_name  from " + tabName + " p left join t_bc_user tbu  on p.applicant = tbu.id where 1=1 ");
        //查看登录人的公司发起的调拨
        if(uid == null|| uid.equals("")){
            throw new Exception("");
        }
        sql.append(" and p.company_id = ?");
        params.add(TokenUtil.getCompanyId(uid));
        //调拨状态
        if (data.get("allocationStatus") != null && !data.get("allocationStatus").equals("")) {
           if(data.get("allocationStatus").equals("0")){ //已创建
               sql.append(" and applicant is null and submit_date is null");
           }
           if(data.get("allocationStatus").equals("1")){//已提交
               sql.append(" and submit_date is not null and EXISTS (select circulation_status from t_scm_approvalflow f" +
                       " where p.current_approval_status = f.circulation_status and f.status ='1'" +
                       " and f.start_status = '开始') ");
           }
           if(data.get("allocationStatus").equals("2")){ //已结束
                sql.append(" and current_approval_status = '结束' ");
           }
        }
        //申请日期起
        if (data.get("applicationStartDate") != null && !data.get("applicationStartDate").equals("")) {
            sql.append(" and substr(p.submit_date,1,10) >= ? ");
            params.add(data.get("applicationStartDate"));
        }
        //申请日期止
        if (data.get("applicationStopDate") != null && !data.get("applicationStopDate").equals("")) {
            sql.append(" and substr(p.submit_date,1,10) <= ? ");
            params.add(data.get("applicationStopDate"));
        }
        //发出仓库
        if (data.get("fromWarehouse") != null && !data.get("fromWarehouse").equals("")) {
            sql.append(" and from_warehouse = ? ");
            params.add(data.get("fromWarehouse"));
        }
        //接受仓库
        if (data.get("aimWarehouse") != null && !data.get("aimWarehouse").equals("")) {
            sql.append(" and aim_warehouse = ? ");
            params.add(data.get("aimWarehouse"));
        }
        //vin查询
        if (data.get("vin") != null && !data.get("vin").equals("")) {
            sql.append(" and EXISTS (select d.allocation_order_id from t_scm_allocation_order_detail d where d.allocation_order_id = p.id and vin like ? ) ");
            params.add("%"+data.get("vin")+"%");
        }
        sql.append(" order by p.create_time desc");
        return this.itScmAllocationOrderDao.page(sql.toString(), params.toArray(), start, limit);
    }
    /**
     * 保存修改
     *
     * @param data
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public void saveOrUpdate(Map data, String uid) throws Exception {
        // Common common = new Common();


        TScmAllocationOrder allocationOrder = new TScmAllocationOrder();
        Map allocationOrderMap = (Map) data.get("allocationOrder");
        List<Map> allocationOrderDetailList = (ArrayList) data.get("allocationOrderDetailList");
        String warehouseId = "";
        boolean flag = true;
        for (Map allocationOrderDetailMap : allocationOrderDetailList) {
            //vin
            if (allocationOrderDetailMap.get("vin") != null && !allocationOrderDetailMap.get("vin").equals("")) {
              List list = this.itScmAllocationOrderDao.list("select * from t_scm_stock where vin =? and company_id=?",allocationOrderDetailMap.get("vin"),TokenUtil.getCompanyId(uid));
              Map map = (Map) list.get(0);
              if(flag){
                  flag = false;
              }else{
                  if(!warehouseId.equals(map.get("warehouseId").toString())){
                      throw new Exception("请选择同一仓库的车辆");
                  }
              }
              warehouseId = map.get("warehouseId").toString();
            }
        }
        /**
         * 调拨单数据绑定
         */
        //目标仓库
        if (allocationOrderMap.get("aimWarehouse") != null && !allocationOrderMap.get("aimWarehouse").equals("")) {
            allocationOrder.setAimWarehouse(allocationOrderMap.get("aimWarehouse").toString());
        }
        //发出仓库
        if (allocationOrderMap.get("fromWarehouse") != null && !allocationOrderMap.get("fromWarehouse").equals("")) {
            allocationOrder.setFromWarehouse(allocationOrderMap.get("fromWarehouse").toString());
        }
        //调拨单编号
        //调拨状态
        if (allocationOrderMap.get("allocationStatus") != null && !allocationOrderMap.get("allocationStatus").equals("")) {
            allocationOrder.setAllocationStatus(allocationOrderMap.get("allocationStatus").toString());
        }
        //调拨类型
        if (allocationOrderMap.get("allocationType") != null && !allocationOrderMap.get("allocationType").equals("")) {
            allocationOrder.setAllocationType(allocationOrderMap.get("allocationType").toString());
        }
        //调拨数量
        if (allocationOrderMap.get("allocationNum") != null && !allocationOrderMap.get("allocationNum").equals("")) {
            allocationOrder.setAllocationNum(allocationOrderMap.get("allocationNum").toString());
        }
        //调拨时间
        if (allocationOrderMap.get("allocationDate") != null && !allocationOrderMap.get("allocationDate").equals("")) {
            allocationOrder.setAllocationDate(allocationOrderMap.get("allocationDate").toString());
        }

        //出库时间
        /*if (allocationOrderMap.get("outWarehouseDate") != null && !allocationOrderMap.get("outWarehouseDate").equals("")) {
            allocationOrder.setOutWarehouseDate(allocationOrderMap.get("outWarehouseDate").toString());
        }*/
        //预计到库时间
        if (allocationOrderMap.get("arriveWarehouseDate") != null && !allocationOrderMap.get("arriveWarehouseDate").equals("")) {
            allocationOrder.setEstimateBackDate(allocationOrderMap.get("arriveWarehouseDate").toString());
        }

        //调拨原因
        if (allocationOrderMap.get("allocationReason") != null && !allocationOrderMap.get("allocationReason").equals("")) {
            allocationOrder.setAllocationReason(allocationOrderMap.get("allocationReason").toString());
        }
        //融资方式
        if(allocationOrderMap.get("rongzi") != null && !allocationOrderMap.get("rongzi").equals("")){
            allocationOrder.setRongzi(allocationOrderMap.get("rongzi").toString());
        }
        //公司id
        if (allocationOrderMap.get("companyId") != null && !allocationOrderMap.get("companyId").equals("")) {
            allocationOrder.setCompanyId(allocationOrderMap.get("companyId").toString());
        }
        //交款金额
        if (allocationOrderMap.get("payMoney") != null && !allocationOrderMap.get("payMoney").equals("")) {
            allocationOrder.setPayMoney(allocationOrderMap.get("payMoney").toString());
        }
        //交款时间
        if (allocationOrderMap.get("payTime") != null && !allocationOrderMap.get("payTime").equals("")) {
            allocationOrder.setPayTime(allocationOrderMap.get("payTime").toString());
        }
        //存放车辆
        if (allocationOrderMap.get("storeCar") != null && !allocationOrderMap.get("storeCar").equals("")) {
            allocationOrder.setStoreCar(allocationOrderMap.get("storeCar").toString());
        }
        //保证金金额
        if (allocationOrderMap.get("depositMoney") != null && !allocationOrderMap.get("depositMoney").equals("")) {
            allocationOrder.setDepositMoney(allocationOrderMap.get("depositMoney").toString());
        }
        //保证金比例
        if (allocationOrderMap.get("depositRatio") != null && !allocationOrderMap.get("depositRatio").equals("")) {
            allocationOrder.setDepositRatio(allocationOrderMap.get("depositRatio").toString());
        }
        allocationOrder.setCreateUser(String.valueOf(allocationOrderMap.get("createUser")));
        allocationOrder.setCreateTime(Calendar.getInstance().getTimeInMillis());
        // if(allocationOrder.getId()==null){
        if (allocationOrderMap.get("id") == null || StringUtils.isEmpty(allocationOrderMap.get("id"))) {
            allocationOrder.setCreateUser(uid);
            allocationOrder.setCreateTime(Calendar.getInstance().getTimeInMillis());
            //申请人
            //allocationOrder.setApplicant(uid);
            allocationOrder.setCompanyId(TokenUtil.getCompanyId(uid));
            //生成调拨流水单号
            allocationOrder.setAllocationOrderNum("DB" + common.currentTime() + common.serialNumber("0"));
            //调拨默认状态
            allocationOrder.setAllocationStatus("0");
        } else {
            String id = String.valueOf(allocationOrderMap.get("id"));
            TScmAllocationOrder temp = new TScmAllocationOrder();
            temp=(TScmAllocationOrder)this.iHibernateDao.findEntity(TScmAllocationOrder.class,id);
            //申请人
            //allocationOrder.setApplicant(uid);
            allocationOrder.setId(temp.getId());
            allocationOrder.setCompanyId(temp.getCompanyId());
            //id不为空时则为修改
            allocationOrder.setUpdateUser(uid);
            allocationOrder.setUpdateTime(Calendar.getInstance().getTimeInMillis());
        }
        this.itScmAllocationOrderDao.saveOrUpdate(allocationOrder);
        //更新流水号
        common.updateSerialNumber(common.serialNumber("0"));
        /**
         * 调拨单明细绑定
         */
        for (Map allocationOrderDetailMap : allocationOrderDetailList) {
            TScmAllocationOrderDetail allocationOrderDetail = new TScmAllocationOrderDetail();
            //vin
            if (allocationOrderDetailMap.get("vin") != null && !allocationOrderDetailMap.get("vin").equals("")) {
                allocationOrderDetail.setVin(allocationOrderDetailMap.get("vin").toString());
                this.itScmAllocationOrderDao.executeSQL("update t_scm_stock set allocation_status = '1' where vin = ?",allocationOrderDetailMap.get("vin"));
            }
            //id
//            if (allocationOrderDetailMap.get("detailId")!=null&&!allocationOrderDetailMap.get("detailId").equals("")) {
//                allocationOrderDetail.setId(allocationOrderDetailMap.get("id").toString());
//            }
            //绑定调拨单id
            allocationOrderDetail.setAllocationOrderId(allocationOrder.getId());
            if (allocationOrderDetailMap.get("detailId") == null) {
                allocationOrderDetail.setAllocationOutTime(0);
                this.itScmAllocationOrderDao.saveOrUpdate(allocationOrderDetail);
            }
        }

    }

    /**
     * 修改调拨单
     * @param data
     * @param uid
     * @throws Exception
     */
    @Override
    public void saveAllocation(Map data,String uid) throws Exception{

        Map allocationOrderMap = (Map) data.get("allocationOrder");
        TScmAllocationOrder allocationOrder = (TScmAllocationOrder) this.iHibernateDao.findEntity(TScmAllocationOrder.class,allocationOrderMap.get("id").toString());
        /**
         * 调拨单数据绑定
         */
        //目标仓库
        if (allocationOrderMap.get("aimWarehouse") != null && !allocationOrderMap.get("aimWarehouse").equals("")) {
            allocationOrder.setAimWarehouse(allocationOrderMap.get("aimWarehouse").toString());
        }
        //发出仓库
        if (allocationOrderMap.get("fromWarehouse") != null && !allocationOrderMap.get("fromWarehouse").equals("")) {
            allocationOrder.setFromWarehouse(allocationOrderMap.get("fromWarehouse").toString());
        }
        //调拨单编号
        //调拨状态
        if (allocationOrderMap.get("allocationStatus") != null && !allocationOrderMap.get("allocationStatus").equals("")) {
            allocationOrder.setAllocationStatus(allocationOrderMap.get("allocationStatus").toString());
        }
        //调拨类型
        if (allocationOrderMap.get("allocationType") != null && !allocationOrderMap.get("allocationType").equals("")) {
            allocationOrder.setAllocationType(allocationOrderMap.get("allocationType").toString());
        }
        //调拨数量
        if (allocationOrderMap.get("allocationNum") != null && !allocationOrderMap.get("allocationNum").equals("")) {
            allocationOrder.setAllocationNum(allocationOrderMap.get("allocationNum").toString());
        }
        //调拨时间
        if (allocationOrderMap.get("allocationDate") != null && !allocationOrderMap.get("allocationDate").equals("")) {
            allocationOrder.setAllocationDate(allocationOrderMap.get("allocationDate").toString());
        }

        //出库时间
        /*if (allocationOrderMap.get("outWarehouseDate") != null && !allocationOrderMap.get("outWarehouseDate").equals("")) {
            allocationOrder.setOutWarehouseDate(allocationOrderMap.get("outWarehouseDate").toString());
        }*/
        //预计到库时间
        if (allocationOrderMap.get("arriveWarehouseDate") != null && !allocationOrderMap.get("arriveWarehouseDate").equals("")) {
            allocationOrder.setEstimateBackDate(allocationOrderMap.get("arriveWarehouseDate").toString());
        }

        //调拨原因
        if (allocationOrderMap.get("allocationReason") != null && !allocationOrderMap.get("allocationReason").equals("")) {
            allocationOrder.setAllocationReason(allocationOrderMap.get("allocationReason").toString());
        }
        //融资方式
        if(allocationOrderMap.get("rongzi") != null && !allocationOrderMap.get("rongzi").equals("")){
            allocationOrder.setRongzi(allocationOrderMap.get("rongzi").toString());
        }
        //公司id
        if (allocationOrderMap.get("companyId") != null && !allocationOrderMap.get("companyId").equals("")) {
            allocationOrder.setCompanyId(allocationOrderMap.get("companyId").toString());
        }
        //交款金额
        if (allocationOrderMap.get("payMoney") != null && !allocationOrderMap.get("payMoney").equals("")) {
            allocationOrder.setPayMoney(allocationOrderMap.get("payMoney").toString());
        }
        //交款时间
        if (allocationOrderMap.get("payTime") != null && !allocationOrderMap.get("payTime").equals("")) {
            allocationOrder.setPayTime(allocationOrderMap.get("payTime").toString());
        }
        //存放车辆
        if (allocationOrderMap.get("storeCar") != null && !allocationOrderMap.get("storeCar").equals("")) {
            allocationOrder.setStoreCar(allocationOrderMap.get("storeCar").toString());
        }
        //保证金金额
        if (allocationOrderMap.get("depositMoney") != null && !allocationOrderMap.get("depositMoney").equals("")) {
            allocationOrder.setDepositMoney(allocationOrderMap.get("depositMoney").toString());
        }
        //保证金比例
        if (allocationOrderMap.get("depositRatio") != null && !allocationOrderMap.get("depositRatio").equals("")) {
            allocationOrder.setDepositRatio(allocationOrderMap.get("depositRatio").toString());
        }

        //id不为空时则为修改
        allocationOrder.setUpdateUser(uid);
        allocationOrder.setUpdateTime(Calendar.getInstance().getTimeInMillis());

        this.itScmAllocationOrderDao.saveOrUpdate(allocationOrder);
    }

    /**
     * 根据调拨单id查询车辆信息
     *
     * @param allocationId
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List carList(String allocationId, String uid) throws Exception {
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" select A.id as detailId,B.* from t_scm_allocation_order_detail A  left join T_Scm_Car B on A.vin ")
                .append(" = B.vin where A.allocation_order_id = ? and B.company_id = ?");
        return this.itScmAllocationOrderDao.list(sqlBuf.toString(), allocationId,TokenUtil.getCompanyId(uid));
    }

    /**
     * 提交或审核
     *
     * @param uid
     * @param id
     * @param status
     * @throws Exception
     */
    @Override
    public void submitOrPass(String uid, String id, String status) throws Exception {
        this.itScmAllocationOrderDao.executeSQL("update t_scm_allocation_order set allocation_status = ? where id = ? ", status, id);
    }

    /**
     * 在库车辆列表查询
     *
     * @param uid
     * @param data
     * @return
     * @throws Exception
     */
    @Override
    public List list(String uid, Map data) throws Exception {
        List params = new ArrayList();
        // Common com = new Common();
        Boolean hasWhere = false;
        StringBuffer sqlbuf = new StringBuffer();
        sqlbuf.append("select * from t_scm_stock where allocation_status='0' ");
        //vin条件模糊
        if (data.get("vin") != null && !data.get("vin").toString().equals("")) {
            hasWhere = common.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append(" vin like ? ");
            params.add("%" + data.get("vin") + "%");
        }
        //车型
        if (data.get("carModel") != null && !data.get("carModel").toString().equals("")) {
            hasWhere = common.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append(" car_model = ? ");
            params.add(data.get("carModel"));
        }
        hasWhere = common.appendWhereIfNeed(sqlbuf, hasWhere);
        sqlbuf.append(" vin not in (select vin from t_scm_allocation_order_detail) ");
        sqlbuf.append(" order by enter_date");
        return this.itScmAllocationOrderDao.list(sqlbuf.toString(), params.toArray());
    }


    /**
     * 审批调拨分页
     *
     * @param data
     * @param start
     * @param limit
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Map approvalPage(Map data, String start, String limit, String uid) throws Exception {

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("select p.*,tbu.u_name  from " + tabName + " p left join t_bc_user tbu  on p.applicant = tbu.id where p.company_id = ?")
                .append(" and current_approval_status<>'结束' and current_approval_status in " +
                        "(SELECT circulation_status from t_scm_approvalflow where audit_roles in " +
                        "(SELECT r_id from t_bc_role_user where u_id = ? ))");
        params.add(TokenUtil.getCompanyId(uid));
        params.add(uid);
        //调拨状态
        if (data.get("allocationStatus") != null && !data.get("allocationStatus").equals("")) {
            sql.append(" and allocation_status = ? ");
            params.add(data.get("allocationStatus"));
        }
        //申请日期起
        if (data.get("applicationStartDate") != null && !data.get("applicationStartDate").equals("")) {
            sql.append(" and DATE_FORMAT(submit_date,'%Y-%m-%d') >= ? ");
            params.add(data.get("applicationStartDate"));
        }
        //申请日期止
        if (data.get("applicationStopDate") != null && !data.get("applicationStopDate").equals("")) {
            sql.append(" and DATE_FORMAT(submit_date,'%Y-%m-%d') <= ? ");
            params.add(data.get("applicationStopDate"));

        }
        //审批申请日期起
        if (data.get("enterTimeStart") != null && !data.get("enterTimeStart").equals("")) {
            sql.append(" and DATE_FORMAT(submit_date,'%Y-%m-%d') >= ? ");
            params.add(data.get("enterTimeStart"));
        }
        //审批申请日期止
        if (data.get("enterTimeStop") != null && !data.get("enterTimeStop").equals("")) {
            sql.append(" and DATE_FORMAT(submit_date,'%Y-%m-%d') <= ? ");
            params.add(data.get("enterTimeStop"));

        }
        //发出仓库
        if (data.get("fromWarehouse") != null && !data.get("fromWarehouse").equals("")) {
            sql.append(" and from_warehouse = ? ");
            params.add(data.get("fromWarehouse"));
        }
        //接受仓库
        if (data.get("aimWarehouse") != null && !data.get("aimWarehouse").equals("")) {
            sql.append(" and aim_warehouse = ? ");
            params.add(data.get("aimWarehouse"));
        }
        sql.append(" order by p.create_time desc");
        return this.itScmAllocationOrderDao.page(sql.toString(), params.toArray(), start, limit);
    }

    /**
     * 更新调拨出库打印次数
     * @param ids
     * @throws Exception
     */
    @Override
    public void addTime(String[] ids) throws Exception{
        StringBuffer updateSql = new StringBuffer();
        updateSql.append("update t_scm_allocation_order_detail set allocation_out_time = allocation_out_time+1 where id in ( ");
        List param = new ArrayList();
        if(ids.length>0) {
            for (String id : ids) {
                updateSql.append(" ? , ");
                param.add(id);
            }
            updateSql.append(" '' ) ");
            this.itScmAllocationOrderDao.executeSQL(updateSql.toString(),param.toArray());
        }
    }

    /**
     * app的审批分页
     * @param data
     * @param start
     * @param limit
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Map appApprovalPage(Map data, String start, String limit, String uid) throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("SELECT p.*,tbu.u_name,case p.allocation_type when '0' then '二网调拨' when '1' then '外展调拨' when '2' then '临时调拨'" +
                " end allocationT,case p.allocation_status when '0' then '已创建' when '1' then '已提交' when '2' then '已结束' end allocationS," +
                "o.o_name, w1.warehouse_name aimW, w2.warehouse_name fromW FROM t_scm_allocation_order p " +
                "LEFT JOIN t_bc_user tbu ON p.applicant = tbu.id left join t_bc_organ o on p.company_id = o.id " +
                "left join t_scm_warehouse w1 on p.aim_warehouse = w1.id left join t_scm_warehouse w2 on p.from_warehouse = w2.id " +
                "WHERE p.company_id = ? and current_approval_status<>'结束' and current_approval_status in " +
                "(SELECT circulation_status from t_scm_approvalflow where audit_roles in " +
                " (SELECT r_id from t_bc_role_user where u_id = ? )) ");

        params.add(TokenUtil.getCompanyId(uid));
        params.add(uid);
        //调拨状态
        if (data.get("allocationStatus") != null && !data.get("allocationStatus").equals("")) {
            sql.append(" and allocation_status = ? ");
            params.add(data.get("allocationStatus"));
        }
        //申请日期起
        if (data.get("applicationStartDate") != null && !data.get("applicationStartDate").equals("")) {
            sql.append(" and DATE_FORMAT(submit_date,'%Y-%m-%d') >= ? ");
            params.add(data.get("applicationStartDate"));
        }
        //申请日期止
        if (data.get("applicationStopDate") != null && !data.get("applicationStopDate").equals("")) {
            sql.append(" and DATE_FORMAT(submit_date,'%Y-%m-%d') <= ? ");
            params.add(data.get("applicationStopDate"));

        }
        //审批申请日期起
        if (data.get("enterTimeStart") != null && !data.get("enterTimeStart").equals("")) {
            sql.append(" and DATE_FORMAT(submit_date,'%Y-%m-%d') >= ? ");
            params.add(data.get("enterTimeStart"));
        }
        //审批申请日期止
        if (data.get("enterTimeStop") != null && !data.get("enterTimeStop").equals("")) {
            sql.append(" and DATE_FORMAT(submit_date,'%Y-%m-%d') <= ? ");
            params.add(data.get("enterTimeStop"));

        }
        //发出仓库
        if (data.get("fromWarehouse") != null && !data.get("fromWarehouse").equals("")) {
            sql.append(" and from_warehouse = ? ");
            params.add(data.get("fromWarehouse"));
        }
        //接受仓库
        if (data.get("aimWarehouse") != null && !data.get("aimWarehouse").equals("")) {
            sql.append(" and aim_warehouse = ? ");
            params.add(data.get("aimWarehouse"));
        }
        sql.append(" order by p.create_time desc");
        return this.itScmAllocationOrderDao.page(sql.toString(), params.toArray(), start, limit);
    }
}
